#! /usr/bin/python
########################################################################   
#    Dooplet is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Dooplet is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Dooplet.  If not, see <http://www.gnu.org/licenses/>.
#######################################################################
#    Authors: Karthikeyan Manivannan
#
######################################################################
import sys, os
import time
import urllib

# dooplet server
serverIP = "www.labdoo.org"
# dooplet server script
pageName = "dooplet"

# file which stores the laptop id
id_file_path = "/usr/dooplet/laptop_id.txt"

# gets  a unique id for the laptop
# either the wired mac address or wireless mac address 
# if neither of these is found then this returns the hostname
# TBD get wireless address

def get_alternate_id():
    id = os.uname()[1] 
    for line in os.popen("/sbin/ifconfig"):
        if 'Ether' in line:
            id = line.split()[4]
            break
    return id    

def get_id():
   if os.path.isfile(id_file_path):
	f = open(id_file_path,'r')
	id = f.readline()
	return id
   return get_alternate_id();

# call home !
laptopId = get_id()
url = "http://" + serverIP + "/" + pageName + "?id=" + laptopId
try:
    response = urllib.urlopen(url)
    print response.read()
except IOError, e:
    print e
